<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="cheken" />
            <span> 江苏传智播客教育科技股份有限公司 </span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list">
          <!-- 在template上使用默认插槽 -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="cheken" />
                <span> {{ data.name }} </span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">
                          添加子部门
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">
                          修改子部门
                        </el-dropdown-item>
                        <el-dropdown-item v-if="data.children && data.children.length===0" @click.native="hDel(data.id)">
                          删除子部门
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
        <el-dialog :title="isEdit ? '修改子部门' : '新增子部门'" :visible.sync="dialogVisible" @close="hclose">
          <addOrEdit :id="curId" ref="myDrpt" :origin-list="originList" :is-edit="isEdit" @success="hSuccess" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeList } from '@/utils'
import addOrEdit from './deptDialog.vue'
export default {
  name: 'DepartmentPage',
  components: {
    addOrEdit
  },
  data() {
    return {
      originList: [],
      dialogVisible: false,
      list: [],
      curId: '',
      isEdit: false
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    hclose() {
      console.log('清空表单')
      this.$refs.myDrpt.resetForm()
    },
    // 点击确定关闭弹窗,刷新数据
    hSuccess() {
      this.dialogVisible = false
      this.loadDepartments()
    },
    async doDel(id) {
      try {
        const res = await delDepartment(id)
        console.log(res)
        // 1. 提示
        this.$message({ type: 'success', message: '删除成功!' })
        // 2. 更新数据
        this.loadDepartments()
      } catch (err) {
        console.log(err)
        this.$message({ type: 'error', message: '删除失败' })
      }
    },
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 做删除
        this.doDel(id)
      }).catch(() => {

      })
    },
    hEdit(id) {
      this.isEdit = true
      this.dialogVisible = true
      this.curId = id
      // this.$nextTick(() => {
      //   this.$refs.myDrpt.loadDepartmentsDetatil()
      // })
    },
    // 子部门
    hAdd(id) {
      this.isEdit = false
      this.dialogVisible = true
      this.curId = id
    },
    async loadDepartments() {
      try {
        const res = await getDepartments()
        this.originList = res.data.depts.map(item => {
          const { code, id, name, pid } = item
          return { code, id, name, pid }
        })
        res.data.depts.shift()
        this.list = tranListToTreeList(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>
</style>
